package com.jr.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uname = req.getParameter("uname");
        String upwd = req.getParameter("upwd");

        if("admin".equals(uname) && "123".equals(upwd)) {
            /*// 1. 设置Session，供拦截过滤器判断登录状态
            req.getSession().setAttribute("user", uname);*/

            System.out.println(req.getSession().getId());
            // 2. 设置Cookie，实现7天免登录
            Cookie cookie = new Cookie("user", uname+":"+upwd);
            cookie.setMaxAge(60*60*24*7); //单位是：秒  ，7天有效期
            /*cookie.setPath("/"); // 整个应用可见*/
            resp.addCookie(cookie);

            req.getSession().setAttribute("user", "user");
            //  登录成功，跳转到首页
            resp.sendRedirect("projectInfo/showdt");
        }else{
            resp.sendRedirect("login.jsp");//重定向
        }
    }
}
